|
Attribues provide a means of changing the behaviour of a method or class. You can setup conditional flags, mark a method as a web method, obsolete or allow a class to be serialized to XML. Attributes are specified in square brackets before the class or methods that they are to apply to. [WebMethod]
public string getPageTitle { return "Test Code"; } WebMethod and WebServiceLets first look at web method, as it was used in the above example. In Web Services, all methods are all hidden from direct access to the Internet. You need to specify, using the WebMethod attribute, any method you want to expose to the Internet. public string HelloWorld()
{ return "Hello World"; } [WebMethod] In the above example, anybody connecting to the web service will only be able to invoke the web_HelloWorld method even though the regular HelloWorld is marked as public. You must also mark the class as a web service using the WebService attribute. This attribute accepts parameters in the form of a namespace. [WebService(Namespace="http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { … In the above example you can see how to apply more than one attribute to a class. SerializableWhen you mark an object as serializable you are informing the compiler that at some point you want to save the object as XML data stream. You can also implement the ISerializable interface. To serialise an object you can use an XML Serializer and StreamWriter to write to a file. using System;
using System.Xml.Serialization; using System.IO; [Serializable] public string
myMethod() class Program XmlSerializer mySerializer = new
XmlSerializer(typeof(myTestClass)); When run this will create an XML document containing the current data of the object. <?xml version="1.0" encoding="utf-8"?>
<myTestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <myString>Hello World</myString> <myInt>1234</myInt> <myArray> <string>qwerty</string> <string>asdfgh</string> <string>zxcvbn</string> <string>123456</string> </myArray> </myTestClass> Deserialize ObjectsDeserialization is the opposite of serialization, reading the data from XML and converting it into an object. The process is very similar to the serialization method above. static void Main()
{ myTestClass test; XmlSerializer mySerializer = new
XmlSerializer(typeof(myTestClass)); test = (myTestClass)mySerializer.Deserialize(myFileStream); // Just to prove it works ConditionalThe conditional attribute can be set on methods so that they can be called only if that condition is true. An example of this is a debug log. When the program is compiled for a debug build, the method will run each time it is called, however when compiled for release the method will never be called. The method is still compiled into the assembly regardless of the target build. class Program
{ [Conditional("DEBUG")] static void debugMethod(string message) { Console.WriteLine(message); } static void
Main() When the program is compiled for debug the console window will show: This is a test This is a debug message This is a test When run for a release build the console window will show: This is a test This is a test ObsoleteThe obsolete attribute is used to mark a method that should not be used any more. It allows for backward compatibility when using assemblies. [Obsolete("This
class is obsolete, please use testClass instead.")]
class oldTestClass { public string Hello() { return "Hello"; } } class testClass class Program This will display a warning message at compile time with the obsolete message. It will not stop any other code from using the method, or break any existing code; it will only show the message to developers. More AttributesThere are many more attributes, you can find them all by inputting a ‘[‘ and invoking IntelliSense to see a list.
Tutorials in this Series
Leave a ReplyClick
here to cancel reply.
|







